package com.shirley.customcontrols;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.shirley.animatedfloatexpandablelistview.entity.GroupData;
import com.shirley.searchfilter.SearchFilterWidget;
import com.shirley.searchfilter.entity.SearchFilterData;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by ZLJ on 2017/12/18.
 * 主界面的Tab2对应的fragment
 */

public class SearchFilterFragment extends Fragment {

    Unbinder unbinder;
    @BindView(R.id.tv)
    TextView tv;

    private SearchFilterWidget mSearchFilterWidget;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_search_filter, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mSearchFilterWidget = new SearchFilterWidget(getContext());
        mSearchFilterWidget.setData(constructData());
        mSearchFilterWidget.setOnCompleteListener(new SearchFilterWidget.OnCompleteListener() {
            @Override
            public void onComplete(List<SearchFilterData> chosenData) {
                if (chosenData != null){
                    String chosen = "";
                    for (SearchFilterData data : chosenData){
                        chosen += data.getData() + "、";
                    }
                    tv.setText(chosen);
                } else {
                    tv.setText("");
                }
            }
        });
    }

    private List<SearchFilterData> constructData() {
        List<SearchFilterData> data = new ArrayList<>();
        // 添加一组多选按钮
        SearchFilterData temp = new SearchFilterData();
        temp.setType(SearchFilterData.MULTI_BUTTON);
        temp.setCategory("类型");
        List<String> contents = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            contents.add("类型 " + i);
        }
        temp.setContent(contents);
        data.add(temp);
        // 添加一组选择时间区间
        SearchFilterData time = new SearchFilterData();
        time.setType(SearchFilterData.TIME_PERIOD);
        time.setCategory("时间区间");
        // 可选参数，修改选择时间控件的颜色
        data.add(time);
        // 添加一组输入框
        SearchFilterData input = new SearchFilterData();
        input.setType(SearchFilterData.INPUT);
        input.setCategory("关键词");
        data.add(input);
        // 添加一组单选列表
        SearchFilterData single = new SearchFilterData();
        single.setType(SearchFilterData.SINGLE_CHOICE);
        single.setCategory("类型选择");
        // 构造单选列表数据
        List<GroupData> singleDataList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            List<GroupData> children = new ArrayList<>();
            for (int j = 0; j < 5; j++) {
                GroupData group = new GroupData();
                group.setName("类别 " + (i + 1) + "-" + (j + 1));
                group.setChildren(null);
                children.add(group);
            }
            GroupData groupData = new GroupData();
            groupData.setName("分类 " + (i + 1));
            groupData.setChildren(children);
            singleDataList.add(groupData);
        }
        single.setContent(singleDataList);
        data.add(single);
        return data;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick(R.id.btn)
    public void onViewClicked() {
        mSearchFilterWidget.switchOpenClose();
    }
}
